import Vue from 'vue'
import VueRouter from 'vue-router'

import View from './view/viewContainer.vue'
import Home from './view/order/Home.vue'
import OrderEdit from './view/order/OrderEdit.vue'
import OrderDetail from './view/order/OrderDetail.vue'
import OrderClassify from './view/order/OrderClassify.vue'
import UserTotal from './view/user/UserTotal.vue'
import UserReceive from './view/user/UserReceive.vue'
import UserRelease from './view/user/UserRelease.vue'

Vue.use(VueRouter)

// 路由规则，实现地址定向
const routes = [
    { path: '/', redirect: '/home' },

    // 前台路由重定向
    {
        path: '/view',
        component: View,
        redirect: '/order',
        children: [
            {
                path: '/home',
                name: 'Home',
                component: Home
            },
            {
                path: '/order/add',
                name: 'OrderAdd',
                component: OrderEdit
            },
            {
                path: '/order/detail/:oid',
                name: 'OrderDetail',
                component: OrderDetail
            },
            {
                path: '/order/classify/:classify',
                name: 'OrderClassify',
                component: OrderClassify
            },
            {
                path: '/user/total',
                name: 'UserTotal',
                component: UserTotal
            },
            {
              path: '/user/receive',
              name: 'UserReceive',
              component: UserReceive
            },
            {
              path: '/user/release',
              name: 'UserRelease',
              component: UserRelease
            }
        ]
    }
]

const router = new VueRouter({
    routes
})

// 挂载路由导航守卫
// 判断token是否存在，如果存在将携带token进行下一簇的操作，如果不存在，则返回登陆
router.beforeEach((to, from, next) => {
    if (to.matched.some((route) => route.meta.Auth)) {
        if (localStorage.getItem('userToken')) {
            next()
        } else {
            next({
                path: 'http://malloc.press:9000/user/nav/toLogin',
                query: {
                    returnURL: to.path
                }
            })
        }
    } else {
        next()
    }
})

export default router
